package com.gmzg.gmzgoffice.report;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.entity.Weekly_report;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class ReportActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "ReportActivity";
    private EditText mEditFinishWork;
    private EditText mEditWorkSummary;
    private EditText mEditNextPlan;
    private EditText mEditNeedHelp;
    private EditText mEditNotes;
    private Button mButtonConfirm;
    private Button mButtonCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
        initView();
        mButtonConfirm.setOnClickListener(this);
        mButtonCancel.setOnClickListener(this);
    }

    private void initView(){
        mEditFinishWork = (EditText) findViewById(R.id.report_add_edit_finish_work);
        mEditWorkSummary = (EditText) findViewById(R.id.report_add_edit_work_summary);
        mEditNextPlan = (EditText) findViewById(R.id.report_add_edit_next_plan);
        mEditNeedHelp = (EditText) findViewById(R.id.report_add_edit_help);
        mEditNotes = (EditText) findViewById(R.id.report_add_edit_notes);
        mButtonConfirm = ((Button) findViewById(R.id.report_add_button_confirm));
        mButtonCancel = ((Button) findViewById(R.id.report_add_button_cancel));
    }

    // response button click
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.report_add_button_confirm:
                addReport();
                break;
            case R.id.report_add_button_cancel:
                cleanData();
                break;
        }
    }

    // add report to Bmob
    public void addReport(){
        String finishWork = mEditFinishWork.getText().toString().trim();
        String workSummary = mEditWorkSummary.getText().toString().trim();
        String nextPlan = mEditNextPlan.getText().toString().trim();
        String needHelp = mEditNeedHelp.getText().toString().trim();
        String notes = mEditNotes.getText().toString().trim();

        SharedPreferences userInfo = getSharedPreferences("userInfo", MODE_PRIVATE);
        String userName = userInfo.getString("user", null);

        //get system time and format it
        SimpleDateFormat simpleDateFormat
                = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String timeStamp = simpleDateFormat.format(date);

        if (!finishWork.equals("")) {
            //upload the report date to Bmob
            Weekly_report report = new Weekly_report();
            report.setFinish_work(finishWork);
            report.setWork_summary(workSummary);
            report.setNext_plan(nextPlan);
            report.setNeed_help(needHelp);
            report.setNotes(notes);
            report.setTimeStamp(timeStamp);
            if (userName != null) {
                report.setUser_name(userName);
            }
            report.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if (e == null) {
                        Toast.makeText(ReportActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                        finish();
//                    queryText();
                    } else {
                        Toast.makeText(ReportActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            Toast.makeText(this, "本周完成工作不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    // to clean data from the field
    public void cleanData(){
        mEditFinishWork.setText("");
        mEditWorkSummary.setText("");
        mEditNextPlan.setText("");
        mEditNeedHelp.setText("");
        mEditNotes.setText("");
    }
}
